package com.mobileClientKernel.android.commonLayer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JSONUtil {
    /**
     * String 转Json
     *
     * @param jsonStr
     * @return
     */
    public static JSONObject getJSONObject(String jsonStr) {
        JSONObject object = null;
        try {
            object = new JSONObject(jsonStr);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return object;
    }

    /**
     * JSON获取JSON
     */
    public static JSONObject getJSONObject(JSONObject object, String key) {
        JSONObject obj = null;
        try {
            obj = object.getJSONObject(key);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return obj;
    }

    /**
     * JSON获取JSON
     */
    public static JSONObject getJSONObject(JSONArray array, int index) {
        JSONObject obj = null;
        try {
            obj = array.getJSONObject(index);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return obj;
    }

    /**
     * 获取String
     *
     * @param object
     * @param key
     * @return
     */
    public static String getString(JSONObject object, String key) {
        String result = null;
        try {
            result = object.getString(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取String
     *
     * @param object
     * @param key
     * @return
     */
    public static String getString(JSONArray object, int key) {
        String result = null;
        try {
            result = object.getString(key);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取int
     *
     * @param object
     * @param key
     * @return
     */
    public static int getInt(JSONObject object, String key) {
        int result = 0;
        try {
            result = object.getInt(key);
        } catch (JSONException e) {
            e.printStackTrace();
            result = -1;
        }
        return result;
    }

    /**
     * 获取Boolean
     *
     * @param object
     * @param key
     * @return
     */
    public static boolean getBoolean(JSONObject object, String key) {
        boolean result = false;
        try {
            result = object.getBoolean(key);
        } catch (JSONException e) {
            e.printStackTrace();
            result = false;
        }
        return result;
    }

    public static JSONArray getJSONArray(JSONObject object, String key) {
        JSONArray array = null;

        try {
            array = object.getJSONArray(key);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return array;
    }

    public static JSONArray getJSONArray(JSONArray object, int key) {
        JSONArray array = null;

        try {
            array = object.getJSONArray(key);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return array;
    }

    public static void put(JSONObject object, String key, Object value) {
        if (null == key || null == value) {
            return;
        }

        try {
            object.put(key, value);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
